<template>
	<div>
		<HomeHeader></HomeHeader>
		<HomeLeft :cur="8"></HomeLeft>
		<div id="main">
			<div id="web_con" v-if="isedit">
				<div class="web_wp">
					<div class="web_location">
						<div class="here_area">
							<strong>当前位置：</strong>项目经理管理&nbsp;&nbsp;»&nbsp;&nbsp;编辑
						</div>
						<div class="here_btn">
							<a href="javascript:history.back()" class="web_btn btn_add"><em
									class="iconfont icon-shdcfanhui"></em>返回</a>
						</div>
					</div>

					<div class="tabTitle">
						<ul class="clearfix">
							<li class="cur"><a href="javascript:;">基本信息</a></li>
						</ul>
						<div class="tabCont">
							<div class="cont">
								<table class="dataTable">
									<tr>
										<td class="ltd">项目经理姓名</td>
										<td class="rtd">
											<input type="text" v-model="name" class="input-text">
											<span class="text-red pl10">*必填</span>
										</td>
									</tr>
									<tr>
										<td class="ltd">手机号</td>
										<td class="rtd">
											<input type="text" v-model="phone" class="input-text">
											<span class="text-red pl10">*必填</span>
										</td>
									</tr>
									<tr>
										<td class="ltd">管理商户</td>
										<td class="rtd">
											 <el-checkbox-group v-model="checklist">
											    <el-checkbox v-for="(item,i) in shoplist" :key="i" :label="item.id">{{item.name}}</el-checkbox>
											  </el-checkbox-group>
										</td>
									</tr>
									
								</table>
							</div>
						</div>
						<div>
							<div class="contBtn">
								<table class="dataTable">
									<tfoot>
										<tr>
											<td class="ltd"></td>
											<td class="rtd">
												<a href="javascript:;" class="web_btn btn_add" @click="SubMit()">
													<em class="iconfont icon-shdcbaocun"></em>保存
												</a>
											</td>
										</tr>
									</tfoot>
								</table>
							</div>
						</div>
					</div>
				</div>

			</div>
		</div>
	</div>
</template>

<script>
	import HomeHeader from '../components/Header.vue';
	import HomeLeft from '../components/Left.vue';
	export default {
		components: {
			HomeHeader,
			HomeLeft
		},
		data() {
			return {
				checklist:[],
				name:'',
				nickname:'',
				phone:'',
				shoplist:{},
				isedit:false,
			}
		},
		onLoad(e) {
			let that = this;
			that.GetShopList();
			if (e.id) {
				that.id = e.id;				
				that.GetDeta(e.id);
			} else {
				that.isedit = true;
			}
		},
		methods: {			
			GetShopList: function() {
				let that = this;
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				let _data = {
					keywords: '',
				};
				that.common.http('GET', 'merchant', _data, function(res) {
					uni.hideLoading();
					console.log(res.data)
					that.shoplist = res.data;
				});
			},
			
			
			// 详情
			GetDeta: function(id) {
				let that = this;
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				that.common.http('GET', 'manager/' + id, '', function(res) {
					uni.hideLoading();
					console.log(res.data);	
					that.name = res.data.name;
					that.phone = res.data.phone;
					that.checklist = res.data.merchants.map(function (item) { return item.id; });
					that.isedit = true;
				});
			},


		
			//保存
			SubMit: function() {
				let that = this;
				console.log(that.checklist)
				
				if (that.name == '') {
					uni.showToast({
						icon: 'none',
						title: '请输入项目经理姓名',
						duration: 2000
					});
				} else if (that.phone == '') {
					uni.showToast({
						icon: 'none',
						title: '请输入手机号',
						duration: 2000
					});
				} 
				else if (that.checklist.length == 0) {
					uni.showToast({
						icon: 'none',
						title: '请选择管理商户',
						duration: 2000
					});
				} 
				else {
					var _data = {
						name: that.name,
						phone: that.phone,
						merchant_ids: that.checklist
					}
					if (that.id > 0) {						
						that.common.http('PUT', 'manager/' + that.id, _data, function(res) {
							uni.hideLoading();
							uni.showToast({
								icon: 'none',
								title: '修改成功！',
								duration: 2000
							});
							setTimeout(function() {
								that.navigateBack();
							}, 1500);
						});
					} else {
						
						that.common.http('POST', 'manager', _data, function(res) {
							uni.hideLoading();
							uni.showToast({
								icon: 'none',
								title: '添加成功',
								duration: 2000
							});
							setTimeout(function() {
								that.navigateBack();
							}, 1500);
						});
					}
				}
			},

			navigateBack() {
				// #ifdef H5
				const pages = getCurrentPages()
				if (pages.length > 1) {
					uni.navigateBack(1)
					return;
				}
				//使用vue-router返回上一级
				let a = this.$router.go(-1)
				if (a == undefined) {}
				return;
				// #endif
				uni.navigateBack();
			},

		}
	}
</script>

<style scoped>
	.el-checkbox{width: 400px;margin: 8px 0;}

</style>